package edu.hawaii.duedates;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * EmailUser contains the function for sending emails to a user.  Email contains 
 * the books due from UHM and HSL system.  
 * @author Anthony Du Vincent Lueng
 */

public class EmailUser {

/**
 * EmailUser takes in String to, String from, and String host and sends a test email
 * to the me (Vince). 
 * @param to contains the to address.
 * @param host contains the host address.
 * @param message contains the email message.
 */
  public EmailUser (String to, String host, String message) {

    Properties props = new Properties();
    String from = "ics413test@gmail.com";
    String subject = "Test E-Mail through Java";
    props.put("mail.smtp.host", host);
 
    Session session = Session.getInstance(props);
  
    try {
      Message msg = new MimeMessage(session);
      msg.setFrom(new InternetAddress(from));
      InternetAddress[] address = { new InternetAddress(to)};
      msg.setRecipients(Message.RecipientType.TO, address);
      msg.setSubject(subject);
      msg.setSentDate(new Date());  
      msg.setText(message);
      Transport.send(msg);
      System.out.println("Email sent.");
    }
    catch (MessagingException mex) {
      mex.printStackTrace();
    } 
  }
}